//
//  LSArrayDataSource.m
//  LibraryStorm
//
//  Created by Enel@Lightning on 4/9/14.
//  Copyright (c) 2014 3ATeam. All rights reserved.
//

#import "LSArrayDataSource.h"

@interface LSArrayDataSource () <UITableViewDataSource>

@property (nonatomic, strong) NSArray *items;
@property (nonatomic, copy) NSString *cellIdentifier;
@property (nonatomic, copy) TableViewCellConfigureBlock configureCellBlock;
@end

@implementation LSArrayDataSource
- (id)init
{
    return nil;
}

/**
 * Method name: initWithItems:cellIdentifier:configureCellBlock
 * Description: Initializer datasource for tableview
 * Parameters: NSArray of items, NSString of cellIdentifier, CompletionBlock
 */
- (id)initWithItems:(NSArray *)anItems
     cellIdentifier:(NSString *)aCellIdentifier
 configureCellBlock:(TableViewCellConfigureBlock)aConfigureCellBlock
{
    self = [super init];
    if (self) {
        self.items = anItems;
        self.cellIdentifier = aCellIdentifier;
        self.configureCellBlock = [aConfigureCellBlock copy];
    }
    return self;
}

/**
 * Method name: itemAtIndexPath
 * Description: Return item at index path
 * Parameters: NSIndexPath
 */
- (id)itemAtIndexPath:(NSIndexPath *)indexPath
{
    return self.items[(NSUInteger) indexPath.row];
}

#pragma mark UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.items.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:self.cellIdentifier forIndexPath:indexPath];
    id item = [self itemAtIndexPath:indexPath];
    self.configureCellBlock(cell, item);
    return cell;
}

@end
